package com.hankol.winecellar

import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.MessageBus
import com.baselibrary.activity.BaseActivity
import com.baselibrary.extension.checkNotNull
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.SpUtils
import com.hankol.winecellar.adapter.GoodsAfterSalesAdapter
import com.hankol.winecellar.bean.GoodsAfterSaleListBean
import com.hankol.winecellar.databinding.ActivityGoodsAfterSalesBinding
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.winecellar.view.ItemDecoration
import com.hankol.winecellar.viewmodel.WineCellarViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.xinlan.imageeditlibrary.editimage.utils.DensityUtil
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**新零售-二期-经营者-售后列表
 * @author: merbng
 * @time: 2022/9/5 14:20
 * @description:
 */
@Route(path = ActivityRouter.WINE_CELLAR.AFTER_SALE_LIST)
class GoodsAfterSaleListActivity : BaseActivity(), View.OnClickListener {
    private lateinit var viewModel: WineCellarViewModel
    private lateinit var binding: ActivityGoodsAfterSalesBinding
    private val mAfterSalesAdapter: GoodsAfterSalesAdapter by lazy { GoodsAfterSalesAdapter() }
    private var page: Int = 1

    override fun getLayoutId(): Int {
        return R.layout.activity_goods_after_sales
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        binding = getDataBinding<ActivityGoodsAfterSalesBinding>()
        viewModel = getActivityViewModelProvider(this).get(WineCellarViewModel::class.java)
        binding.rvAfterSalesList.adapter = mAfterSalesAdapter
        binding.rvAfterSalesList.addItemDecoration(ItemDecoration(DensityUtil.dip2px(this, 14f), DensityUtil.dip2px(this, 8f)))
        mAfterSalesAdapter.setOnItemClickListener { adapter, view, position ->
            //跳转售后详情
            val mDataBean = adapter.data[position] as GoodsAfterSaleListBean
            val hasMap: HashMap<String, Any> = hashMapOf()
            hasMap[GoodsAfterSalesDetailsActivity.AFTER_SALES_ID] = mDataBean.afterSaleId
            ARouterUtils.jump(ActivityRouter.WINE_CELLAR.AFTER_SALE_DETAIL, hasMap)
        }
        reqData()
        initObserve()
        initListener()
    }

    fun reqData() {
        viewModel.reqGoodsAfterSaleList(SpUtils.getInstance().currentWineryInfo.dealerCode, page)
    }

    fun initObserve() {
        viewModel.afterSaleListLD.observe(this, Observer {
            binding.refreshAfterSales.finishRefresh()
            binding.refreshAfterSales.finishLoadMore()
            if (it.data.checkNotNull()) {
                if (page <= 1) {
                    mAfterSalesAdapter.setNewData(it.data)
                } else {
                    mAfterSalesAdapter.addData(it.data)
                }
                page++
            } else {
                if (page <= 1) {
                    mAfterSalesAdapter.setNewData(arrayListOf())
                    val emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_ORDER_LIST, binding.rvAfterSalesList, "暂无售后订单")
                    mAfterSalesAdapter.emptyView = emptyView
                }
            }
        })
    }

    fun initListener() {
        binding.ivBack.setOnClickListener(this)
        binding.refreshAfterSales.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
                reqData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                reqData()
            }
        })
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN) fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgId_retail_after_sales_revoke) {

        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> {
                finish()
            }
        }
    }
}